<?php

namespace App\Models;

use App\Services\CommonService;
use App\Services\GoodsService;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class GoodsSetmeal extends Model
{
    use HasFactory;
    protected $table = 'sp_goods_setmeal_2';
    protected $primaryKey = 'id';
    public $timestamps = false;

    //is_delete是否删除  1是  2否
    const GOODS_SETMEAL_IS_DELETE = 1;//是
    const GOODS_SETMEAL_NOT_DELETE = 2;//否

    protected function GetRentType($goods_id)
    {
        $data = $this->where([['goods_id', $goods_id],['is_delete',GoodsService::GOODS_NO_DELETE_STATUS]])
            ->select('lease_set')
            ->get();
        $data = $data ? $data->toArray() : array();
        if($data){
            $data = array_column($data,'lease_set');
            if(in_array(GoodsService::GOODS_RENT_TYPE_MONTH,$data)){//按月
                return 1;
            }elseif (in_array(GoodsService::GOODS_RENT_TYPE_DAY,$data)){//按天
                return 2;
            }
        }else{
            return '';
        }

    }

    //获取商品租赁类型
    protected function GoodsRentType($goods_id)
    {
        $data = $this->where([['goods_id', $goods_id],['is_delete',GoodsService::GOODS_NO_DELETE_STATUS]])
            ->select('lease_set')
            ->get();
        $data = $data ? $data->toArray() : array();
        if($data){
            $data = array_column($data,'lease_set');
            return $data;
        }else{
            return '';
        }
    }

    //小程序检测套餐
    protected function CheckGoodsSetmeal($goods_id, $setmeal_id)
    {
        $setmeal = $this->where([['goods_id', $goods_id], ['id', $setmeal_id], ['is_delete', GoodsService::GOODS_NO_DELETE_STATUS]])
            ->select('lease_set', 'title', 'foregift', 'is_buy_out', 'price', 'start_date', 'buyout_price')->first();
        $setmeal = $setmeal ? $setmeal->toArray() : [];
        return $setmeal;
    }

    //判断小程序是否支持花呗
    protected function getGoodsHuabei($goods_id){
        $huabei_count = $this->where([['goods_id',$goods_id],['is_huabei',GoodsService::GOODS_SETMEAL_IS_HUABEI],['is_delete',GoodsService::GOODS_NO_DELETE_STATUS]])->count();
        $is_haubei = $huabei_count > 0 ? GoodsService::GOODS_SETMEAL_IS_HUABEI : GoodsService::GOODS_SETMEAL_NO_HUABEI;
        return $is_haubei;
    }


    //获取套餐列表
    protected function getGoodsSetmeal($goods_id){
        $data = $this->where([['goods_id',$goods_id],['is_delete',GoodsSetmeal::GOODS_SETMEAL_NOT_DELETE]])
            ->select('id','goods_id','title','lease_set','quantity','foregift','price','start_date','is_buy_out','is_huabei','buyout_price')
            ->get();
        $data = $data ? $data->toArray() : array();
        return $data;
    }

    //获取套餐
    protected function getGoodsSetmealInfo($setmeal_id){
        $data = $this->where([['id',$setmeal_id],['is_delete',GoodsService::GOODS_NO_DELETE_STATUS]])
            ->select('id','goods_id','title','lease_time','lease_set','quantity','foregift','price','start_date','is_buy_out','is_huabei','buyout_price')
            ->first();
        $data = $data ? $data->toArray() : array();
        return $data;
    }

    //获取套餐租期
    protected function getGoodsMinLeaseTerm($goods_id){
        $data = $this->where([['goods_id',$goods_id],['is_delete',GoodsService::GOODS_NO_DELETE_STATUS]])
            ->select('id','lease_set')
            ->get();
        $data = $data ? $data->toArray() : array();
        $min_lease_term = 0;
        if(!empty($data)){
            $min_lease_term_arr = [];
            foreach ($data as $k=>$v){
                $min_lease_term_arr[] = GoodsSpec::GetSetmealMinLeaseTerm($goods_id,$v['id'],$v['lease_set']);
            }
            $min_lease_term = min($min_lease_term_arr);
        }
        return $min_lease_term;
    }

    //获取最高押金
    protected function getMaxForegift($goods_id){
        $max_foregift = $this->where([['goods_id', $goods_id],['is_delete',GoodsSetmeal::GOODS_SETMEAL_NOT_DELETE]])
            ->max('foregift');
        return $max_foregift;
    }

    //添加套餐
    protected function AddSetmeal($request){
        if($request['operation_type'] && $request['operation_type'] != GoodsService::GOODS_SAVE_DRAFT) { //保存草稿箱不验证数据
            if(empty($request['quantity'])){
                return ['status'=>CommonService::HTTP_CODE_FAIL,'msg'=>'请填写套餐库存','data'=>[]];
            }
            if(empty($request['foregift'])){
                return ['status'=>CommonService::HTTP_CODE_FAIL,'msg'=>'商品押金不能为空','data'=>[]];
            }
            if(empty($request['price'])){
                return ['status'=>CommonService::HTTP_CODE_FAIL,'msg'=>'商品租售价格不能为空','data'=>[]];
            }
            if(empty($request['buyout_price'])){
                return ['status'=>CommonService::HTTP_CODE_FAIL,'msg'=>'商品买断价格不能为空','data'=>[]];
            }
            if(empty($request['is_buy_out'])){
                return ['status'=>CommonService::HTTP_CODE_FAIL,'msg'=>'请选择套餐是否可买断','data'=>[]];
            }
            if(empty($request['spec'])){
                return ['status'=>CommonService::HTTP_CODE_FAIL,'msg'=>'租期信息设置不能为空','data'=>[]];
            }
            if($request['is_huabei'] ==  GoodsService::GOODS_SETMEAL_IS_HUABEI && empty($request['huabei_data'])){
                return ['status'=>CommonService::HTTP_CODE_FAIL,'msg'=>'花呗信息设置不能为空','data'=>[]];
            }
        }
        if(empty($request['title'])){
            return ['status'=>CommonService::HTTP_CODE_FAIL,'msg'=>'套餐名称不能为空','data'=>[]];
        }
        if(empty($request['lease_set'])){
            return ['status'=>CommonService::HTTP_CODE_FAIL,'msg'=>'请选择套餐租期类型','data'=>[]];
        }
        if ($request['operation_type'] && $request['operation_type'] == GoodsService::GOODS_PUBLISH){
            Goods::where('id',$request['goods_id'])->update(['ischeck'=>GoodsService::GOODS_CHECK_STATUS_NO]);
        }

        $data = [];
        $data['goods_id'] = $request['goods_id'];
        $data['title'] = $request['title'];
        $data['lease_set'] = $request['lease_set'];
        $data['buyout_price'] = $request['buyout_price']??0;
        $data['quantity'] = $request['quantity'];
        $data['foregift'] = $request['foregift'];
        $data['price'] = $request['price'];
        $data['lease_time'] = $request['lease_time'];
        $data['start_date'] = !empty($request['start_date'])?$request['start_date']:3;
        $data['is_buy_out'] = $request['is_buy_out'];
        $data['is_huabei'] = !empty($request['is_huabei'])?$request['is_huabei']:GoodsService::GOODS_SETMEAL_NO_HUABEI;
        $data['is_delete'] = CommonService::GOODS_IS_DELETE_NOT;
        $data['create_time'] = time();
        $setmeal_id = $this->insertGetId($data);
        if($setmeal_id){
            if(!empty($request['spec'])){
                GoodsSpec::AddGoodsSpec($request['spec'],$request['goods_id'],$setmeal_id,$request['lease_set'],$request['lease_time']);
            }
            return ['status'=>CommonService::HTTP_CODE_SUCCESS,'msg'=>'添加成功','data'=>['goods_id'=>$request['goods_id'],'setmeal_id'=>$setmeal_id]];
        }else{
            return ['status'=>CommonService::HTTP_CODE_FAIL,'msg'=>'添加失败','data'=>[]];
        }

    }


    //编辑套餐
    protected function EditGoodsSetmeal($request){
        $setmeal = $this->where([['id',$request['setmeal_id']],['is_delete',CommonService::GOODS_IS_DELETE_NOT]])->select('id','goods_id')->first();
        $setmeal = $setmeal ? $setmeal->toArray() : array();
        if(empty($setmeal)){
            return ['status'=>CommonService::HTTP_CODE_FAIL,'msg'=>'商品套餐不存在','data'=>[]];
        }
        if($request['operation_type'] && $request['operation_type'] != GoodsService::GOODS_SAVE_DRAFT) { //保存草稿箱不验证数据
            if(empty($request['quantity'])){
                return ['status'=>CommonService::HTTP_CODE_FAIL,'msg'=>'请填写套餐库存','data'=>[]];
            }
            if(empty($request['foregift'])){
                return ['status'=>CommonService::HTTP_CODE_FAIL,'msg'=>'商品押金不能为空','data'=>[]];
            }
            if(empty($request['price'])){
                return ['status'=>CommonService::HTTP_CODE_FAIL,'msg'=>'商品租售价格不能为空','data'=>[]];
            }
            if(empty($request['is_buy_out'])){
                return ['status'=>CommonService::HTTP_CODE_FAIL,'msg'=>'请选择套餐是否可买断','data'=>[]];
            }
            if(empty($request['spec'])){
                return ['status'=>CommonService::HTTP_CODE_FAIL,'msg'=>'租期信息设置不能为空','data'=>[]];
            }
            if(empty($request['buyout_price'])){
                return ['status'=>CommonService::HTTP_CODE_FAIL,'msg'=>'买断价不能为空','data'=>[]];
            }
            if($request['is_huabei'] ==  GoodsService::GOODS_SETMEAL_IS_HUABEI && empty($request['huabei_data'])){
                return ['status'=>CommonService::HTTP_CODE_FAIL,'msg'=>'花呗信息设置不能为空','data'=>[]];
            }
        }
        if(empty($request['title'])){
            return ['status'=>CommonService::HTTP_CODE_FAIL,'msg'=>'套餐名称不能为空','data'=>[]];
        }
        if(empty($request['lease_set'])){
            return ['status'=>CommonService::HTTP_CODE_FAIL,'msg'=>'请选择套餐租期类型','data'=>[]];
        }
        $goods = Goods::where([['id',$request['goods_id']],['is_delete',CommonService::GOODS_IS_DELETE_NOT]])->select('id','status','ischeck')->first();
        $goods = $goods ? $goods->toArray() : array();
        if ($request['operation_type'] && $request['operation_type'] == GoodsService::GOODS_PUBLISH){
            Goods::where('id',$request['goods_id'])->update(['ischeck'=>GoodsService::GOODS_CHECK_STATUS_NO]);
        }else{
            if($goods['ischeck'] >= GoodsService::GOODS_CHECK_STATUS_CHECKED){
                Goods::where('id',$request['goods_id'])->update(['ischeck'=>GoodsService::GOODS_CHECK_STATUS_NOT_SUBMIT]);
            }
        }

        $data = [];
        $data['title'] = $request['title'];
        $data['lease_set'] = $request['lease_set'];
        $data['buyout_price'] = $request['buyout_price'];
        $data['quantity'] = $request['quantity'];
        $data['foregift'] = $request['foregift'];
        $data['price'] = $request['price'];
        $data['lease_time'] = $request['lease_time'];
        $data['start_date'] = $request['start_date']>=0?$request['start_date']:3;
        $data['is_buy_out'] = $request['is_buy_out'];
        $data['is_huabei'] = !empty($request['is_huabei'])?$request['is_huabei']:GoodsService::GOODS_SETMEAL_NO_HUABEI;
        $this->where('id',$request['setmeal_id'])->update($data);
        if(!empty($request['spec'])){
            GoodsSpec::EditGoodsSpec($request['spec'],$request['goods_id'],$request['setmeal_id'],$request['lease_set'],$request['lease_time']);
        }
        return ['status'=>CommonService::HTTP_CODE_SUCCESS,'msg'=>'添加成功','data'=>['goods_id'=>$request['goods_id'],'setmeal_id'=>$request['setmeal_id']]];
    }

    //删除套餐
    protected function DeleteStemeal($goods_id,$member_id,$setmeal_id){
        $goods = Goods::where([['id',$goods_id],['member_id',$member_id],['is_delete',GoodsService::GOODS_NO_DELETE_STATUS]])
            ->select('id','status','ischeck')->first();
        $goods = $goods ? $goods->toArray() : [];
        if(!empty($goods)){
            $count = $this->where([['goods_id',$goods_id],['id',$setmeal_id],['is_delete',GoodsService::GOODS_NO_DELETE_STATUS]])->count();
            if($count > 0){
                $goodsData = [];
                if($goods['status'] == GoodsService::GOODS_STATUS_PUT){
                    $goodsData['status'] = GoodsService::GOODS_STATUS_OFF;
                }
                if($count == 1){
                    $goodsData['ischeck'] = GoodsService::GOODS_CHECK_STATUS_NOT_SUBMIT;
                    Goods::where('id',$goods_id)->update($goodsData);
                }else{
                    if(!empty($goodsData)){
                        Goods::where('id',$goods_id)->update($goodsData);
                    }
                }
                $res = $this->where([['id',$setmeal_id],['goods_id',$goods_id]])->update(['is_delete'=>GoodsService::GOODS_DELETE_STATUS]);
                GoodsSpec::where([['setmeal_id',$setmeal_id],['goods_id',$goods_id]])->update(['is_delete'=>GoodsService::GOODS_DELETE_STATUS]);
                if($res !==false ){
                    return ['status'=>CommonService::HTTP_CODE_SUCCESS,'msg'=>'操作成功'];
                }else{
                    return ['status'=>CommonService::HTTP_CODE_FAIL,'msg'=>'操作失败'];
                }
            }else{
                return ['status'=>CommonService::HTTP_CODE_FAIL,'msg'=>'套餐不存在'];
            }
        }else{
            return ['status'=>CommonService::HTTP_CODE_FAIL,'msg'=>'商品不存在'];
        }
    }

}
